﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ICTBilling;

public partial class WebForms_Standard : System.Web.UI.Page
{
    private static List<ExceptionHandler> ExecutionCollection = new List<ExceptionHandler>();
    protected void Page_Load(object sender, EventArgs e)
    {
        MultiView1.SetActiveView(ShowView);
    }
    protected void SearchImageButton_Click(object sender, ImageClickEventArgs e)
    {
        StandardGridView.PageSize = (PageSizeDropDownList.SelectedIndex == -1 || PageSizeDropDownList.SelectedValue == "0" ? 5 : Convert.ToInt32(PageSizeDropDownList.SelectedValue));
        if (CenterNameDropDownList.SelectedValue != "0" ||
            TypeDropDownList.SelectedValue != "0")
        {
            StandardFormView.ChangeMode(FormViewMode.Insert);
            ClearImageButton.OnClientClick = string.Empty;
        }
        else
            ClearImageButton_Click(sender, e);
    }
    protected void ClearImageButton_Click(object sender, ImageClickEventArgs e)
    {
        CenterNameDropDownList.SelectedValue = TypeDropDownList.SelectedValue = "0";
        StandardFormView.ChangeMode(FormViewMode.Insert);
        ClearImageButton.OnClientClick = "return false;";
    }
    protected void NewImageButton_Click(object sender, ImageClickEventArgs e)
    {
        MultiView1.SetActiveView(EditView);
        StandardFormView.ChangeMode(FormViewMode.Insert);
        StandardFormView.DataBind();
    }
    protected void StandardGridView_SelectedIndexChanged(object sender, EventArgs e)
    {
        StandardFormView.PageIndex = (StandardGridView.PageSize * StandardGridView.PageIndex) + StandardGridView.SelectedIndex;
        StandardFormView.ChangeMode(FormViewMode.Edit);
        StandardFormView.DataBind();
        if (((DropDownList)StandardFormView.FindControl("TypeDropDownList")).SelectedValue == "2")
        {
            ((Label)StandardFormView.FindControl("MinPerPersonLabel1")).Text = ((Label)StandardFormView.FindControl("MaxPerPersonLabel1")).Text = ((Label)StandardFormView.FindControl("MinEquipmentLabel1")).Text = ((Label)StandardFormView.FindControl("MaxEquipmentLabel1")).Text = "کیلو وات";
        }
        else
        {
            ((Label)StandardFormView.FindControl("MinPerPersonLabel1")).Text = ((Label)StandardFormView.FindControl("MaxPerPersonLabel1")).Text = ((Label)StandardFormView.FindControl("MinEquipmentLabel1")).Text = ((Label)StandardFormView.FindControl("MaxEquipmentLabel1")).Text = "متر مکعب";
        }

        MultiView1.SetActiveView(EditView);
        StandardGridView.SelectedIndex = -1;
    }
    protected void StandardEntityDataSource_Command(object sender, EntityDataSourceChangedEventArgs e)
    {
        tbStandard newsItem = e.Entity as tbStandard;
        if (e.Entity != null)
            ExecutionCollection.AddRange(newsItem.Validate(StandardFormView.CurrentMode == FormViewMode.Edit ? ChangeAction.Update : ChangeAction.Insert));
        StandardGridView.DataBind();
        MultiView1.SetActiveView(ShowView);
    }

    protected void CalculateLinkButton_Click(object sender, EventArgs e)
    {
        ICTBillingEntities _ICTBillingEntities = new ICTBillingEntities();
        int CenterID = int.Parse(((DropDownList)StandardFormView.FindControl("CenterDropDownList")).SelectedValue);
        ObjectParameter totalUsage = new ObjectParameter("TotalUsage", typeof(int));
        _ICTBillingEntities.spCenterEquipmentStandard(CenterID, totalUsage);
        ((TextBox)StandardFormView.FindControl("MaxEquipmentTextBox")).Text = Math.Round(1.02 * int.Parse(totalUsage.Value.ToString())).ToString();
        ((TextBox)StandardFormView.FindControl("MinEquipmentTextBox")).Text = Math.Round(0.98 * int.Parse(totalUsage.Value.ToString())).ToString();
        MultiView1.SetActiveView(EditView);
    }

}